<?php
/**
 * Created by PhpStorm.
 * User: HLG
 * Date: 2019/10/21
 * Time: 11:20
 */
namespace app\api\model;
class Treasure extends BaseModel
{
    protected $table = 'yoshop_treasure';

    /**
     * 获取宝藏列表
     *
     * @param $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getTreasureList($user_id)
    {
        $page = input('page')?:1;
        $list = $this->where('state',0)->page($page,4)->select();
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }

    /**
     * 用户夺取记录
     *
     * @param $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getTreasureLog($user_id)
    {
        $treasureModel = new TreasureUser();
        $page = input('page')?:1;
        $list = $treasureModel->where('state',1)->page($page,20)->select();
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }

    /**
     * 获取打造宝藏需要的币数量
     *
     * @param $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getHube($user_id)
    {
        $treasureConfigModel = new TreasureConfig();
        $list = $treasureConfigModel->getList();
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }

    /**
     * 打造宝藏
     *
     * @param $user_id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function makeTreasure($user_id)
    {
        $config_id = input('post.id');
        //获取打造信息
        $treasureConfig = new TreasureConfig();
        $configInfo = $treasureConfig->getInfo(['id'=>$config_id]);
        //判断币是否充足
        checkBalance($user_id,$configInfo['make']);
        $this->startTrans();
        //生成宝藏
        $insertRes = $this->insertInfo([
            'user_id'=>$user_id,
            'hube_number'=>$configInfo['make'],
            'consume_number'=>$configInfo['consume'],
            'hube'=>$configInfo['make'],
            'state'=>0,
            'create_time' => time()
        ]);
        //扣除用户hube并增加日志
        $balanceModel = new Balance();
        $changeBalance = $balanceModel->changeBalance($user_id,$configInfo['make'],2,3,'打造宝藏扣除HUBE：'.$configInfo['make']);
        if($insertRes && $changeBalance){
            $this->commit();
            $status = ['code'=>1,'msg'=>'打造成功','data'=>''];
        }else{
            $this->rollback();
            $status = ['code'=>0,'msg'=>'打造失败','data'=>''];
        }
        return $status;
    }

    /**
     * 夺取宝藏
     *
     * @param $user_id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function seizeTreasure($user_id)
    {
        $treasure_id = input('post.id');//宝藏id
        $data = $this->getInfo(['id'=>$treasure_id]);
        if($data['user_id'] == $user_id){
            AjaxReturn(0,'不能夺取自己的宝藏');
        }
        //判断资金
        checkBalance($user_id,$data['consume_number']);
        $this->startTrans();
        //扣除资金
        $balanceModel = new Balance();
        $decBalance = $balanceModel->changeBalance($user_id,$data['consume_number'],2,4,'夺取宝藏消耗HUBE：'.$data['consume_number']);
        //总概率
        $total = $data['hube']+$data['consume_number'];
        $probability = $data['consume_number']/$total*100;
        $result = rand(1,100);
        if($result<=$probability){//中奖
            //计算手续费
            $settingModel = new Setting();
            $config = $settingModel->getSetting('treasure',10001);
            $fee = $total * $config['seize'];
            $profit = $total;
            $state = 1;
            //扣手续费进余额
            $incBalance = $balanceModel->changeBalance($user_id,$total-$fee,1,4,'夺取宝藏赢取HUBE：'.$total.'，扣除手续费：'.$fee);
            //更改宝藏状态
            $updateState = $this->updateInfo(['id'=>$treasure_id],['state'=>1]);
            $uodateNumber = true;
            $status = ['code'=>1,'msg'=>'中奖啦'];
        }else{//未中奖
            $fee = 0;
            $profit = '-'.$data['consume_number'];
            $state = 0;
            $incBalance = true;
            $updateState = true;
            //更改宝藏中币的数量
            $uodateNumber = $this->updateNumber(['id'=>$treasure_id],1,'hube',$data['consume_number']);
            $status = ['code'=>0,'msg'=>'未中奖'];

        }
        $treasureLogModel = new TreasureUser();
        //添加夺宝记录
        $log = $treasureLogModel->insertInfo([
            'user_id'=>$user_id,
            'treasure_id'=>$treasure_id,
            'hube'=>$data['consume_number'],
            'profit'=>$profit,
            'charge' => $fee,
            'state' => $state,
            'create_time' => time()
        ]);
        if($decBalance && $incBalance && $log && $updateState && $uodateNumber){
            $this->commit();
        }else{
            $this->rollback();
            $status = ['code'=>-1,'msg'=>'操作失败'];
        }
        return $status;
    }

    /**
     * 我的宝藏
     *
     * @param $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myTreasure($user_id)
    {
        $page = input('page')?:1;
        $list = $this->where(['state'=>0,'user_id'=>$user_id])->page($page,10)->select();
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }

    /**
     * 收取宝藏
     *
     * @param $user_id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function collect($user_id)
    {
        $id = input('post.id'); //宝藏id
        $data = $this->getInfo(['id'=>$id,'state'=>0,'user_id'=>$user_id]);
        if($data){
            if($data['hube']==$data['hube_number']){
                $fee = 0;
            }else{
                //计算手续费
                $settingModel = new Setting();
                $config = $settingModel->getSetting('treasure',10001);
                $fee = $config['collect'] * $data['hube'];
            }
            $number = $data['hube'] - $fee;
            $this->startTrans();
            //更改宝藏状态
            $updateState = $this->updateInfo(['id'=>$id],['state'=>2]);
            //更改用户余额
            $balanceModel = new Balance();
            $changeBlance = $balanceModel->changeBalance($user_id,$number,1,5,'收取宝藏获取HUBE：'.$data['hube'].'，扣除手续费：'.$fee);
            if($updateState && $changeBlance){
                $this->commit();
                $status = ['code'=>1,'msg'=>'收取成功'];
            }else{
                $this->rollback();
                $status = ['code'=>0,'msg'=>'操作失败'];
            }

        }else{
            $status = ['code'=>0,'msg'=>'宝藏不存在或已被收取'];
        }
        return $status;
    }

    /**
     * 我的夺宝历史
     *
     * @param $user_id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myTreasureLog($user_id)
    {
        $page = input('page');
        $list = $this->alias('t')
            ->field('tu.hube,u.mobile,t.hube_number,tu.state,tu.create_time')
            ->join('treasure_user tu','t.id=tu.treasure_id')
            ->join('user u','u.user_id=tu.user_id')
            ->where('t.user_id',$user_id)
            ->page($page,10)
            ->select();
        if($list){
            $status = ['code'=>1,'msg'=>'查询成功','data'=>$list];
        }else{
            $status = ['code'=>0,'msg'=>'暂无数据','data'=>''];
        }
        return $status;
    }
}